package day13;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: 王丹
 * @create: 2021-07-06 09:48
 **/
//await(long time, TimeUnit unit)超时之后自动返回
public class Demo5 {
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static class T extends Thread {
        public void run() {
            lock.lock();
            try {
                System.out.println(System.currentTimeMillis() + "," + this.getName() + ":start");
                boolean await = condition.await(2, TimeUnit.SECONDS);
                System.out.println(await);
                System.out.println(System.currentTimeMillis() + "," + this.getName() + ":end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println("lock.unlock()");
            }
        }
    }

    /**
     * 结果分析：
     * t1线程等待2秒之后，自动返回继续执行，最后await方法返回false，await返回false表示超时之后自动
     * 返回
     */
    public static void main(String[] args) throws InterruptedException {
        T t1 = new T();
        t1.setName("t1");
        t1.start();
    }
}
